import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('../views/login/index.vue')
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('../views/layout/index.vue'),
    redirect: '/userlist',
    children: [
      {
        path: '/userlist',
        name: 'userlist',
        component: () => import('../views/userlist/index.vue')
      },
      {
        path: '/addgoods',
        name: 'addgoods',
        component: () => import('../views/addCom/index.vue')
      },
      {
        path: '/datareport',
        name: 'logdatareportin',
        component: () => import('../views/datareport/index.vue')
      },
      {
        path: '/goodslist',
        name: 'goodslist',
        component: () => import('../views/goodslist/index.vue')
      },
      {
        path: '/goodsort',
        name: 'goodsort',
        component: () => import('../views/goodsort/index.vue')
      },
      {
        path: '/orderlist',
        name: 'orderlist',
        component: () => import('../views/orderlist/index.vue')
      },
      {
        path: '/purviewlist',
        name: 'purviewlist',
        component: () => import('../views/purviewlist/index.vue')
      },
      {
        path: '/querylist',
        name: 'querylist',
        component: () => import('../views/querylist/index.vue')
      },
      {
        path: '/rolelist',
        name: 'rolelist',
        component: () => import('../views/rolelist/index.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
import { getToken, delToken } from '@/utils/storage.js'

//路由守卫  地址发生改变时触发
const white = ['/']
// 路由导航守卫--路由钩子函数 路由地址发生改变就会触发钩子函数
//前置路由守卫
router.beforeEach((to, from, next) => {
  let token = getToken()
  if (token) {
    next()
  } else {
    if (white.includes(to.path)) {
      next()
    } else {

      next('/')
      // next({ path: '/ligin' })
    }
  }

  //from.path  :当前路由地址
  //to.path :前往的路由地址

})
export default router
